<?php 
defined('RUN_KATANA') or die('Restricted access');
class UserContact extends Object
{
	public $UserId;
	public $Name;
	public $LastName;
	public $Email;
	public $Phone;
	
	public function getUserId() { return intval($this->UserId); }
	public function setUserId($val) { $this->UserId=intval($val); }
	
	public function UserContact($values = Array())
	{ 
		self::setDataTable('user_contacts');
		if(count($values) > 0)
		{ self::setValues($values); }
	}
	
	public function __toString()
	{
		if(self::getName() != '')
		{ $out .= self::getName().' '; }
		if(self::getLastName() != '')
		{ $out .= self::getLastName().' '; }
		if(self::getEmail() != '')
		{ $out .= self::getEmail().' '; }
		$out .= self::getPhone();
		return $out;
	}
	
	public function insert()
	{
		if(self::getUserId() > 0)
		{
			$sql  = 'INSERT INTO '.DB_PERFIX.self::getDataTable();
			$sql .= '(`userId`, `name`, `lastName`, `email`, `phone`) '; 
			$sql .= 'VALUES ';
			$sql .= '('.self::getUserId().', ';
			$sql .= '"'.self::formatText(self::getName()).'", ';
			$sql .= '"'.self::formatText(self::getLastName()).'", ';
			$sql .= '"'.self::formatText(self::getEmail()).'", ';
			$sql .= '"'.self::formatText(self::getPhone()).'"';
			$sql .= ');';
			$result = mysql_query($sql);
		}
	}
	
	public function update()
	{
		if((self::getId() > 0) && (self::getDataChanged()))
		{
			global $user;
			if(($user->getId() == self::getUserId()) || ($user->getLevel() > 4))
			{
				$sql  = 'UPDATE '.DB_PERFIX.self::getDataTable().' SET ';
				$sql .= '`userId`='.self::getUserId().', ';
				$sql .= '`name`="'.self::formatText(self::getName()).'", ';
				$sql .= '`lastName`="'.self::formatText(self::getLastName()).'", ';
				$sql .= '`email`="'.self::formatText(self::getEmail()).'", ';
				$sql .= '`phone`="'.self::formatText(self::getPhone()).'" ';
				$sql .= ' WHERE id='.self::getId().' LIMIT 1;';
				mysql_query($sql);
			}
		}
	}
}
?>